home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_STR.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  7KB  |  183 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/String.h>
  14. #include <test.h>
  15.  
  16.  
  17. void test_string() {
  18.   CoolString s1;
  19.   TEST ("CoolString s1", 0, 0);
  20.   TEST ("strlen(s1) == 0", strlen (s1), 0);
  21.   s1 = "Hello";
  22.   TEST ("s1 = \"Hello\"", strlen (s1), 5);
  23.   TEST ("strlen(s1) == 5", strlen (s1), 5);
  24.   TEST ("strcmp(s1,\"Hello\")", strcmp(s1,"Hello"), 0);
  25.   CoolString s5("World",5L);
  26.   TEST ("CoolString s5(\"World\",5)", 0, 0);
  27.   CoolString s2("World");
  28.   TEST ("CoolString s2(\"World\")", 0, 0);
  29.   TEST ("strlen(s2) == 5", strlen (s2), 5);
  30.   TEST ("strcmp(s2,\"World\")", strcmp(s2,"World"), 0);
  31.   s2 = s2 + '!';
  32.   TEST ("s2 = s2 + '!'", strcmp(s2,"World!"), 0);
  33.   strcat (s1, " ");
  34.   TEST ("strcat(s1, \" \")", strlen (s1), 6);
  35.   s1 += s2;
  36.   TEST ("s1 += s2", strcmp (s1,"Hello World!"), 0);
  37.   TEST ("strcmp (s1,\"Hello World!\")", strcmp (s1,"Hello World!"), 0);
  38.   TEST ("s1 == \"Hello World!\"", s1 == "Hello World!", 1);
  39.   TEST ("is_equal(s2,\"WORLD\",SENSITIVE)", is_equal(s2,"WORLD",INSENSITIVE),
  40.     FALSE);
  41.   TEST ("is_equal(s2,\"WORLD!\",INSENSITIVE)",
  42.     is_equal(s2,"WORLD!",INSENSITIVE), TRUE);
  43.   TEST ("strlen (s1) == 12", strlen (s1), strlen ("Hello World!"));
  44.   TEST ("s1[6] == 'W'", (s1[6] == 'W'), 1);
  45.   TEST ("s1 != s2", (s1 != s2), TRUE);
  46.   CoolString s3 = s2;
  47.   TEST ("s3 = s2", (s3 == s2), TRUE);
  48.   s1.reverse ();
  49.   TEST ("s1.reverse()", is_equal (s1, "!DLROW OLLEH", INSENSITIVE), TRUE);
  50.   TEST ("upcase (s2)", strcmp (upcase(s2),"WORLD!"), 0);
  51.   TEST ("downcase (s2)", strcmp (downcase(s2), "world!"), 0);
  52.   TEST ("is_lt (s2, \"WORLD!\")", is_lt (s2, "WORLD!", SENSITIVE),
  53.     FALSE);
  54.   TEST ("is_le (s2, \"WORLD!\")",
  55.     is_le (s2, "WORLD!", INSENSITIVE), TRUE);
  56.   s1.reverse ();
  57.   TEST ("downcase (s1)", strcmp (downcase(s1), "hello world!"), 0);
  58.   TEST ("capitalize (s1)", strcmp (capitalize (s1), "Hello World!"), 0);
  59.   left_trim (s1, "Hello ");
  60.   TEST ("left_trim (s1, \"Hello \")", is_equal (s1, s2, INSENSITIVE), TRUE);
  61.   right_trim (s1, "!");
  62.   TEST ("right_trim (s1, \"!\")", is_equal (s1, "WORLD", INSENSITIVE), TRUE);
  63.   downcase (s1);
  64.   TEST ("is_equal (s1, \"world\", SENSITIVE)",
  65.     is_equal (s1, "world", SENSITIVE), TRUE);
  66.   trim (s1, "or");
  67.   TEST ("trim (s1, \"or\")", strcmp (s1, "wld"), 0);
  68.   s1.clear();
  69.   TEST ("s1.clear()", strlen(s1), 0);
  70.   s1 += "12345";
  71.   TEST ("s1 += \"12345\"", strcmp (s1, "12345"), 0);
  72.   TEST ("strtol (s1)", (strtol(s1) == (long)12345), 1);
  73.   TEST ("atol (s1)", (atol(s1) == (long)12345), 1);
  74.   TEST ("itol (s1)", (atoi(s1) == (int)12345), 1);
  75.   s1 += '.';
  76.   TEST ("s1 += '.'", strcmp (s1, "12345."), 0);
  77.   s1 = s1 + "7";
  78.   TEST ("s1 = s1 + \"7\"", strcmp (s1,"12345.7"), 0); 
  79.   TEST ("atof (s1)", (atof(s1) == (double)12345.7), 1);
  80.   TEST ("strtod (s1)", (strtod(s1) == (double)12345.7), 1);
  81.   s1 = "my string";
  82.   CoolString s33(s1);
  83.   Boolean ans = s1.insert("big ",3);
  84.   TEST ("s1.insert(\"big \",3)",strcmp(s1,"my big string"), 0);
  85.   TEST ("return value",ans==TRUE,1);
  86.   TEST ("s33==\"my string\"",strcmp(s33,"my string"),0);
  87.   ans = s1.insert("big ",-1);
  88.   TEST ("return value",ans==FALSE,1);
  89.   ans = s1.insert("big ", 14);
  90.   TEST ("return value",ans==FALSE,1);
  91.   ans = s1.insert("big ",13);
  92.   TEST ("s1.insert(\"big\",13)",strcmp(s1,"my big stringbig "), 0);
  93.   TEST ("return value",ans==TRUE,1);
  94.  
  95.   s1 = "my string";
  96.   CoolString s32(s1);
  97.   ans = s1.remove(3,5);
  98.   TEST ("s1.remove(3,5)",strcmp(s1,"my ring"), 0);
  99.   TEST ("return value",ans==TRUE,1);
  100.   TEST ("s32==\"my string\"",strcmp(s32,"my string"),0);
  101.   TEST ("return value",s1.remove(4,-1),FALSE);
  102.   TEST ("return value",s1.remove(-1,4),FALSE);
  103.   TEST ("return value",s1.remove(4,4),FALSE);
  104.   TEST ("return value",s1.remove(6,4),FALSE);
  105.   TEST ("return value",s1.remove(8,4),FALSE);
  106.   TEST ("return value",s1.remove(2,8),FALSE);
  107.   ans = s1.remove(5,7);
  108.   TEST ("s1.remove(5,7)",strcmp(s1,"my ri"), 0);
  109.   TEST ("return value",ans==TRUE,1);
  110.   ans = s1.remove(0,1);
  111.   TEST ("s1.remove(0,1)",strcmp(s1,"y ri"), 0);
  112.   TEST ("return value",ans==TRUE,1);
  113.  
  114.   s1 = "my string";
  115.   TEST ("s1.replace(\"bo\",3,5)", s1.replace("bo",3,5), TRUE);
  116.   TEST ("strcmp(s1,\"my boring\")", strcmp (s1,"my boring"), 0);
  117.   TEST ("s1.replace(\" very\",2,3)",s1.replace(" very ",2,3), TRUE);
  118.   TEST ("strcmp (s1,\"my very boring\")",strcmp(s1,"my very boring"), 0);
  119.   TEST ("s1.replace(\"my,\",3,7)",s1.replace("my,",3,7), TRUE);
  120.   TEST ("strcmp(s1,\"my my, boring\")",strcmp(s1,"my my, boring"), 0);
  121.   TEST ("s1.replace(\"nothing\",3,14)",s1.replace("nothing",3,14), FALSE);
  122.   TEST ("s1.replace(\"ed\",10,13)",s1.replace("ed",10,13), TRUE);
  123.   TEST ("strcmp(s1,\"my my, bored\")",strcmp(s1,"my my, bored"), 0);
  124.   TEST ("s1.replace(\"b\",0,1)",s1.replace("b",0,1), TRUE);
  125.   TEST ("strcmp(s1,\"by my, bored\")",strcmp(s1,"by my, bored"), 0);
  126.  
  127.   CoolString temp;
  128.   s1 = "my string";
  129.   CoolString s30(s1);
  130.   s1.yank(temp,3,5);
  131.   TEST ("s1.yank(3,5)",strcmp(s1,"my ring"), 0);
  132.   TEST ("return value",strcmp(temp,"st"),0);
  133.   TEST ("s30==\"my string\"",strcmp(s30,"my string"),0);
  134.   s1.yank(temp,5,7);
  135.   TEST ("s1.yank(5,7)",strcmp(s1,"my ri"), 0);
  136.   TEST ("return value",strcmp(temp,"ng"),0);
  137.   s1.yank(temp,0,1);
  138.   TEST ("s1.yank(0,1)",strcmp(s1,"y ri"), 0);
  139.   TEST ("return value",strcmp(temp,"m"),0);
  140.  
  141.   s1 = "my string";
  142.   CoolString answ;
  143.   CoolString s20(s1);
  144.   s1.sub_string(answ,3,5);
  145.   TEST ("s1.sub_string(3,5)",strcmp(s1,"my string"), 0);
  146.   TEST ("return value",strcmp(answ,"st"),0);
  147.   TEST ("s20==\"my string\"",strcmp(s20,"my string"),0);
  148.   s1.sub_string(answ,5,7);
  149.   TEST ("s1.sub_string(5,7)",strcmp(s1,"my string"), 0);
  150.   TEST ("return value",strcmp(answ,"ri"),0);
  151.   s1.sub_string(answ,0,1);
  152.   TEST ("s1.sub_string(0,1)",strcmp(s1,"my string"), 0);
  153.   TEST ("return value",strcmp(answ,"m"),0);
  154.  
  155.   s1 = "garbage to be deleted";
  156.   CoolString s19(s1);
  157.   answ = strncpy(s1,"hello there",5);
  158.   TEST ((char*)(const char*) s1,0,0);
  159.   TEST ("strncpy(3,5)",strcmp(s1,"hello"), 0);
  160.   TEST ("return value",strcmp(answ,"hello"),0);
  161.   TEST ("s19==\"garbage to be deleted\"",strcmp(s19,"garbage to be deleted"),0);
  162.   TEST ((const char*) s1,0,0);
  163.   answ = strncpy(s1,"hi",5);
  164.   TEST ("strncpy(s1,\"hi\",5)",strcmp(s1,"hi"), 0);
  165.   TEST ("return value",strcmp(answ,"hi"),0);
  166. }
  167.  
  168. void test_leak() {
  169.   for (;;) {
  170.     test_string();
  171.   }
  172. }
  173.  
  174. int main (void) {
  175.   START("CoolString");
  176.   test_string();
  177. #if LEAK
  178.   test_leak();
  179. #endif
  180.   SUMMARY();
  181.   return 0;
  182. }
  183.